﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class StringValidator
    {


        public static bool IsBlank(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "You must specify a value for this required field.");
                rule.IsValid = Field.Value.ToString().Trim().Length >= 1;
                return rule.IsValid;
            }
            catch
            {
                rule.ValidationMessage = "You must specify a value for this required field.";
                rule.IsValid = false;
                return false;
            }
        }

        public static bool MaxLength(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Maximum number of characters allowed for " + Field.DisplayName + " is " + rule.ValidateAgainst + ".");
                return Field.Value.ToString().Length <= int.Parse(rule.ValidateAgainst);
            }
            catch
            {
                return false;
            }
        }


        public static bool MinLength(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Minimum number of characters required for " + Field.DisplayName + " is " + rule.ValidateAgainst + ".");
                return Field.Value.ToString().Length >= int.Parse(rule.ValidateAgainst);
            }
            catch
            {
                return false;
            }
        }

        public static bool FixedLength(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Number of characters for " + Field.DisplayName + " must be exactly " + rule.ValidateAgainst + ".");
                return Field.Value.ToString().Length == int.Parse(rule.ValidateAgainst);
            }
            catch
            {
                return false;
            }
        }

        public static bool Equal(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Value of  " + Field.DisplayName + " must be equal to " + rule.ValidateAgainst + ".");
                return Field.Value.ToString().ToLower() == rule.ValidateAgainst.ToLower();
            }
            catch
            {
                return false;
            }
        }

        public static bool NotEqual(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Value of " + Field.DisplayName + " should not be equal to " + rule.ValidateAgainst + ".");
                return Field.Value.ToString().ToLower() != rule.ValidateAgainst.ToLower();
            }
            catch
            {
                return false;
            }
        }

        public static bool Contains(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Value of " + Field.DisplayName + " must contain " + rule.ValidateAgainst + ".");
                return Field.Value.ToString().ToLower().Contains(rule.ValidateAgainst.ToLower());
            }
            catch
            {
                return false;
            }
        }

        public static bool DoesNotContains(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Value of  " + Field.DisplayName + " should not contain " + rule.ValidateAgainst + ".");
                return !Field.Value.ToString().ToLower().Contains(rule.ValidateAgainst.ToLower());
            }
            catch
            {
                return false;
            }
        }

        public static bool RegExMatch(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Invalid Input data.");
                Regex regex = new Regex(@rule.ValidateAgainst);
                return regex.IsMatch(Field.Value.ToString());
            }
            catch
            {
                return false;
            }

        }
    }
}
